package org.arain.baseii.upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.FileUtils;



public class CommonUtil {
	public static final int BUFFER_SIZE = 8192;

	/**
	 * 根据InputStream保存文件
	 * 
	 * @param is
	 * @param path文件位置
	 * @param maxSize
	 */
	public static void saveFileByInputStream(InputStream is, String path,
			long maxSize) {
		File tmpFile = getTmpFile();
		byte[] dataBuf = new byte[2048];
		BufferedInputStream bis = new BufferedInputStream(is, BUFFER_SIZE);
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(tmpFile), BUFFER_SIZE);

			int count = 0;
			while ((count = bis.read(dataBuf)) != -1) {
				bos.write(dataBuf, 0, count);
			}
			bos.flush();
			bos.close();
			bis.close();
			if (tmpFile.length() > maxSize) {
				tmpFile.delete();
			}
			saveMoveFile(tmpFile, path);
			tmpFile.delete();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据byte[]保存文件
	 * 
	 * @param data
	 * @param path文件位置
	 */
	public static void saveBinaryFile(byte[] data, String path) {
		File file = new File(path);
		try {
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(file));
			bos.write(data);
			bos.flush();
			bos.close();
		} catch (IOException ioe) {
			throw new RuntimeException("文件操作失败");
		}
	}


	/**
	 * 临时创建的文件
	 * 
	 * @return
	 */
	private static File getTmpFile() {
		File tmpDir = FileUtils.getTempDirectory();
		String tmpFileName = (Math.random() * 10000 + "").replace(".", "");
		return new File(tmpDir, tmpFileName);
	}

	/**
	 * 根据文件目录将原文件移动到新的文件中
	 * 
	 * @param tmpFile
	 *            原文件
	 * @param path
	 *            目标文件目录
	 */
	public static void saveMoveFile(File tmpFile, String path) {
		File targetFile = new File(path);
		if (targetFile.canWrite()) {
			throw new RuntimeException("信息错误");
		}
		try {
			FileUtils.moveFile(tmpFile, targetFile);
		} catch (IOException e) {
			throw new RuntimeException("IO错误");
		}
	}
	/**
	 * 根据文件目录将原文件移动到新的文件中
	 * 
	 * @param tmpFile
	 *            原文件
	 * @param path
	 *            目标文件目录
	 */
	public static void saveMoveFile(String tmpPath, String path) {
		File targetFile = new File(path);
		if (targetFile.canWrite()) {
			throw new RuntimeException("权限不足");
		}
		try {
			FileUtils.moveFile(new File(tmpPath), targetFile);
		} catch (IOException e) {
			throw new RuntimeException("IO错误");
		}
	}

	/**
	 * 将原文件复制到目标文件中
	 * 
	 * @param tmpFile
	 *            原文件
	 * @param path
	 *            目标文件
	 */
	public static void saveCopyFile(File tmpFile, String path) {
		File targetFile = new File(path);
		if (targetFile.canWrite()) {
			throw new RuntimeException("权限不足");
		}
		try {
			FileUtils.copyFile(tmpFile, targetFile);
		} catch (IOException e) {
			throw new RuntimeException("IO错误");
		}
	}
	/**
	 * 将原文件复制到目标文件中
	 * 
	 * @param tmpFile
	 *            原文件
	 * @param path
	 *            目标文件
	 */
	public static void saveCopyFile(String tmpPath, String path) {
		File targetFile = new File(path);
		if (targetFile.canWrite()) {
			throw new RuntimeException("权限不足");
		}
		try {
			FileUtils.copyFile(new File(tmpPath), targetFile);
		} catch (IOException e) {
			throw new RuntimeException("IO错误");
		}
	}

	/**
	 * 根据文件目录删除文件
	 * 
	 * @param path
	 */
	public static void deleteFile(String path) {
		File targetFile = new File(path);
		if (!targetFile.exists()) {
			return;
		}
		if (targetFile.isDirectory()) {
			try {
				FileUtils.deleteDirectory(targetFile);
			} catch (IOException e) {
				throw new RuntimeException("IO错误");
			}
		} else if (targetFile.isFile()) {
			targetFile.delete();
		} else {
			throw new RuntimeException("IO错误");
		}

	}

	/**
	 * 根据文件删除文件
	 * 
	 * @param file
	 */
	public static void deleteFile(File file) {
		if (file.canWrite()) {
			throw new RuntimeException("权限不足");
		}
		if (file.isDirectory()) {
			try {
				FileUtils.deleteDirectory(file);
			} catch (IOException e) {
				throw new RuntimeException("IO错误");
			}
		} else if (file.isFile()) {
			file.delete();
		} else {
			throw new RuntimeException("IO错误");
		}
	}
	
}
